package o2sjsc.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	String URL = "http://webbi.vn/WebServices/TechsService.asmx";
	String GET_LIST_LATES_NEWS = "MGetListLatestNews";
	String GET_LIST_HOT_NEWS = "MGetListHotNews";
	String GET_LIST_NEWS_BY_CATEGORY = "MGetListNewsByCategory";
	String GET_LIST_NEW_BY_SEARCH = "MGetListNewsBySearch";

	HashMap<String, String> paramInput;
	Button btnHome, btnBusi, btnTech, btnRecr, btnViewM, btnMenu, btnMenuHome,btnLoadMore;
	ProgressBar progressBar;
	ListView listView;
	ImageButton imgLenTop;
	ListView lv;
	LinearLayout linearLayout;
	ArrayList<Button> listButton;
	TextView tvUpdateData;
	int startIndex = 1;
	int nbRecords = 5;
	String cateogryId = "";
	ArrayList<HashMap<String, String>> output;
	Activity activity = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ActionBar actionBar = getActionBar();

		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.activity_main_action_bar);
		btnMenu = (Button) findViewById(R.id.btnAction);
		btnMenu.setOnClickListener(this);

		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
		output = new ArrayList<HashMap<String, String>>();

		linearLayout = (LinearLayout) findViewById(R.id.relative);
		// Button business
		btnBusi = (Button) findViewById(R.id.btnBusi);
		btnBusi.setText(ValueConstants.BUSINESS);
		btnBusi.setTextSize(13);
		btnBusi.setTypeface(null, Typeface.BOLD);
		btnBusi.setOnClickListener(this);

		// Button technology
		btnTech = (Button) findViewById(R.id.btnTech);
		btnTech.setText(ValueConstants.TECHCOLOY);
		btnTech.setTextSize(13);
		btnTech.setTypeface(null, Typeface.BOLD);
		btnTech.setOnClickListener(this);

		// Button recruitment
		btnRecr = (Button) findViewById(R.id.btnRecr);
		btnRecr.setText(ValueConstants.RECRUITMENT);
		btnRecr.setTextSize(13);
		btnRecr.setTypeface(null, Typeface.BOLD);
		btnRecr.setOnClickListener(this);

		listButton = new ArrayList<Button>();
		listButton.add(btnBusi);
		listButton.add(btnTech);
		listButton.add(btnRecr);

		btnHome = (Button) findViewById(R.id.btnHome);
		btnHome.setOnClickListener(this);
		btnViewM = (Button) findViewById(R.id.btnViewM);
		btnViewM.setTextSize(13);
		btnViewM.setTypeface(null, Typeface.BOLD);
		btnViewM.setOnClickListener(this);

		tvUpdateData = (TextView)findViewById(R.id.lblUpdate);
		
		btnLoadMore = new Button(this);
		btnLoadMore.setText(ValueConstants.LOADMORE);
		imgLenTop = new ImageButton(this);
		imgLenTop.setImageResource(R.drawable.len_top);
		//btnLoadMore.setVisibility(View.VISIBLE);
		//imgLenTop.setVisibility(View.VISIBLE);
		listView.addFooterView(imgLenTop);
		listView.addFooterView(btnLoadMore);
		imgLenTop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listView.setSelectionFromTop(0, 0);
			}
		});
		btnLoadMore.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startIndex = startIndex + nbRecords;
				if (!cateogryId.equals("")) {
					getListNewsByCategory(cateogryId,
							String.valueOf(startIndex),
							String.valueOf(nbRecords));
					return;
				} else {
					loadDataByHome(String.valueOf(startIndex),
							String.valueOf(nbRecords));
					return;
				}
			}
		});
		loadDataByHome(String.valueOf(startIndex), String.valueOf(nbRecords));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAction:
			final Dialog dg = new Dialog(this);
			dg.requestWindowFeature(Window.PROGRESS_START);
			dg.getWindow().setBackgroundDrawable(
					new ColorDrawable(Color.TRANSPARENT));
			dg.setContentView(R.layout.activity_main_dialog_menu);
			dg.setCancelable(true);
			dg.getWindow().setLayout(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			Button btnMenuHome = (Button) dg.findViewById(R.id.btnMenuHome);
			btnMenuHome.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dg.dismiss();
				}
			});

			Button btnMenuGiaVang = (Button) dg.findViewById(R.id.btnMenuIntro);
			btnMenuGiaVang.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dg.dismiss();
				}
			});

			Button btnContact = (Button) dg.findViewById(R.id.btnMenuInfo);
			btnContact.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					final Dialog dgContact = new Dialog(activity);
					dgContact.requestWindowFeature(Window.FEATURE_NO_TITLE);
					dg.getWindow().setBackgroundDrawable(
							new ColorDrawable(Color.TRANSPARENT));
					dgContact.setContentView(R.layout.dialog_contact);
					dgContact.getWindow().setLayout(LayoutParams.MATCH_PARENT,
							LayoutParams.MATCH_PARENT);
					String infoWebsite = "<html><body></body><a href='http://o2s.vn/'>Visit o2s.vn!</a></html>";
					WebView wvInfoWebsite = (WebView) dgContact
							.findViewById(R.id.wvInfoWebsite);
					wvInfoWebsite.loadData(infoWebsite, "text/html", "utf-8");
					dg.dismiss();
					dgContact.show();
				}
			});

			Button btnIntro = (Button) dg.findViewById(R.id.btnMenuIntro);
			btnIntro.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					final Dialog dgInfo = new Dialog(activity);
					dgInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
					dg.getWindow().setBackgroundDrawable(
							new ColorDrawable(Color.TRANSPARENT));
					dgInfo.setContentView(R.layout.dialog_introduction);
					dgInfo.getWindow().setLayout(LayoutParams.MATCH_PARENT,
							LayoutParams.MATCH_PARENT);
					String infoWebsite = "<html><body><span><a href='http://webbi.vn'>"
							+ R.string.companyName
							+ "</a></span></body></html>";
					WebView wvInfoWebsite = (WebView) dgInfo
							.findViewById(R.id.wvInfoWebsite);
					wvInfoWebsite.loadData(infoWebsite, "text/html", "utf-8");
					dg.dismiss();
					dgInfo.show();
				}
			});

			Button btnExit = (Button) dg.findViewById(R.id.btnMenuExit);
			btnExit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					exitApplication();
				}
			});

			dg.show();

			break;
		case R.id.btnHome:
			for (Button bt : listButton) {
				bt.setTextColor(Color.BLACK);
			}
			output = new ArrayList<HashMap<String, String>>();
			startIndex = 1;
			nbRecords = 5;
			cateogryId = "";
			loadDataByHome(String.valueOf(startIndex),
					String.valueOf(nbRecords));
			break;
		case R.id.btnViewM:
			output = new ArrayList<HashMap<String, String>>();
			startIndex = 1;
			nbRecords = 5;
			Intent intent = new Intent(MainActivity.this,
					CactegoryActivity.class);
			startActivityForResult(intent, GET_CODE);
			break;
		default:
			output = new ArrayList<HashMap<String, String>>();
			startIndex = 1;
			nbRecords = 5;
			Button button = (Button) v;
			loadListNewByCateName(button);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int postion,
			long id) {
		TextView tvId = (TextView) view.findViewById(R.id.contentId);
		Intent i = new Intent(this, DetailsActivity.class);
		Bundle b = new Bundle();
		b.putString("contentId", tvId.getText().toString());
		i.putExtras(b);
		startActivity(i);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitApplication();
			return true;
		} else {
			return false;
		}
	}

	public void exitApplication() {
		new AlertDialog.Builder(activity)
				.setIcon(R.drawable.logo)
				.setTitle(R.string.app_name)
				.setNegativeButton(R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								MainActivity.this.finish();
							}
						}).setPositiveButton(R.string.cancel, null).show();
	}

	// Check existed button in menu scroll
	public Button checkExistButton(String cateName) {
		for (Button bt : listButton) {
			if (bt.getText().toString().equals(cateName)) {
				return bt;
			}
		}
		return null;
	}

	static final private int GET_CODE = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == GET_CODE) {
			if (resultCode == RESULT_OK) {
				addButton(data.getAction());
			}
		}
	}

	public void addButton(String cateName) {
		if (!cateName.equals(ValueConstants.HOME)) {
			if (checkExistButton(cateName) != null) {
				Button bt = checkExistButton(cateName);
				bt.setTextColor(Color.parseColor("#054BA0"));
				loadListNewByCateName(bt);
			} else {
				Button bt1 = new Button(this);
				View v = new View(this);
				bt1.setText(cateName);
				bt1.setTextSize(13);
				bt1.setTypeface(null, Typeface.BOLD);
				bt1.setBackgroundResource(R.drawable.gradient_button_bg);
				v.setBackgroundResource(R.drawable.bg4);

				listButton.add(bt1);
				bt1.setOnClickListener(this);
				loadListNewByCateName(bt1);
				linearLayout.addView(bt1);
				linearLayout.addView(v);
			}
		} else {
			for (Button bt : listButton) {
				bt.setTextColor(Color.BLACK);
			}
			loadDataByHome(String.valueOf(startIndex),
					String.valueOf(nbRecords));
		}
	}

	public void loadListNewByCateName(Button button) {
		for (Button bt : listButton) {
			bt.setTextColor(Color.BLACK);
		}
		Button bt = checkExistButton(button.getText().toString());
		if (bt != null) {
			bt.setTextColor(Color.parseColor("#054BA0"));
			String cateName = bt.getText().toString();
			if (cateName.equals(ValueConstants.BUSINESS)) {
				cateogryId = "31890";
			} else if (cateName.equals(ValueConstants.TECHCOLOY)) {
				cateogryId = "31892";
			} else if (cateName.equals(ValueConstants.RECRUITMENT)) {
				cateogryId = "31925";
			} else if (cateName.equals(ValueConstants.MOBILE)) {
				cateogryId = "31896";
			} else if (cateName.equals(ValueConstants.SCIENCE)) {
				cateogryId = "31904";
			} else if (cateName.equals(ValueConstants.SCURITY)) {
				cateogryId = "31909";
			} else if (cateName.equals(ValueConstants.IT)) {
				cateogryId = "31914";
			} else if (cateName.equals(ValueConstants.INTERNET)) {
				cateogryId = "31916";
			} else if (cateName.equals(ValueConstants.COMPUTER)) {
				cateogryId = "31919";
			}
			getListNewsByCategory(cateogryId, String.valueOf(startIndex),
					String.valueOf(nbRecords));
		}
	}

	public void loadDataByHome(String startIndex, String maxRecord) {
		paramInput = new HashMap<String, String>();
		paramInput.put("startIndex", startIndex);
		paramInput.put("maxRecords", maxRecord);
		paramInput.put("portalId", "23079");
		new NewsAsyncTask(this, listView, paramInput, URL, GET_LIST_LATES_NEWS,
				output,tvUpdateData).execute();
	/*	if(output.size()>=4){
			btnLoadMore.setVisibility(View.VISIBLE);
			imgLenTop.setVisibility(View.VISIBLE);
		}else{
			btnLoadMore.setVisibility(View.GONE);
			imgLenTop.setVisibility(View.GONE);
		}*/
	}

	public void getListNewsByCategory(String cateId, String startIndex,
			String maxRecord) {
		paramInput = new HashMap<String, String>();
		paramInput.put("categoryId", cateId);
		paramInput.put("startIndex", startIndex);
		paramInput.put("maxRecords", maxRecord);
		paramInput.put("portalId", "23079");
		new NewsAsyncTask(this, listView, paramInput, URL,
				GET_LIST_NEWS_BY_CATEGORY, output, tvUpdateData).execute();
		/*if(output.size()>=4){
			btnLoadMore.setVisibility(View.VISIBLE);
			imgLenTop.setVisibility(View.VISIBLE);
		}else{
			btnLoadMore.setVisibility(View.GONE);
			imgLenTop.setVisibility(View.GONE);
		}*/
	}
}
